/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.FasilitasDAO;
import com.ivanbiz.dao.impl.FasilitasDAOImpl;
import com.ivanbiz.model.Fasilitas;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.service.ServiceHelper;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class FasilitasPanel extends javax.swing.JPanel {

    Kamar kamar;
    List<Fasilitas> listFasilitas;
    NumberFormat numberFormat;
    JDialog jDialog;
    Fasilitas fasilitas;

    public FasilitasPanel(JDialog jDialog) {
        initComponents();
        this.jDialog = jDialog;
        listFasilitas = new ArrayList<Fasilitas>();
        updateTableFasilitas();
    }

    public FasilitasPanel(JDialog jDialog, Fasilitas fasilitas) {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        this.fasilitas = fasilitas;
        this.kamar = fasilitas.getKamar();
        textKamar.setText(fasilitas.getKamar().getKodeKamar());
        refresh();
    }

    public FasilitasPanel() {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        listFasilitas = new ArrayList<Fasilitas>();
        updateTableFasilitas();
    }

    public FasilitasPanel(Kamar kamar) {
        initComponents();
        numberFormat = NumberFormat.getCurrencyInstance();
        this.kamar = kamar;
        buttonCari.setVisible(false);
        textKamar.setText(kamar.getKodeKamar());
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textKamar = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        buttonTambah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableFasilitas = new javax.swing.JTable();
        buttonCari = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Daftar Fasilitas");

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Kamar :");

        textKamar.setEditable(false);

        jLabel3.setText("Fasilitas :");

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });

        tableFasilitas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableFasilitas);

        buttonCari.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonTambah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonHapus))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(textKamar, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCari)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKamar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCari))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonHapus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonTambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 363, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        if (textKamar.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Kamar belum dipilih");
        } else {
            JDialog jDialogFasilitas = new JDialog();
            jDialogFasilitas.setModal(true);
            jDialogFasilitas.setSize(435, 435);
            jDialogFasilitas.add(new ItemFasilitasPanel(jDialogFasilitas, listFasilitas, kamar));
            jDialogFasilitas.setLocationRelativeTo(null);
            jDialogFasilitas.setVisible(true);
            numberFormat = NumberFormat.getCurrencyInstance();
            refresh();
        }
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tableFasilitas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Fasilitas fasilitass = listFasilitas.get(tableFasilitas.getSelectedRow());
                FasilitasDAO fasilitasDAO = new FasilitasDAOImpl();
                fasilitasDAO.delete(fasilitass);
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(FasilitasPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed

    private void buttonCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariActionPerformed
        JDialog jDialogKos = new JDialog();
        jDialogKos.setModal(true);
        jDialogKos.setSize(800, 600);
        KamarPanel kamarPanel = new KamarPanel(jDialogKos, "fasilitas");
        jDialogKos.add(kamarPanel);
        jDialogKos.setLocationRelativeTo(null);
        jDialogKos.setVisible(true);
        this.kamar = kamarPanel.getKamarFasilitas();
        textKamar.setText(kamarPanel.getKamarFasilitas() == null ? null : kamarPanel.getKamarFasilitas().getKodeKamar());
        refresh();
    }//GEN-LAST:event_buttonCariActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCari;
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableFasilitas;
    private javax.swing.JTextField textKamar;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            FasilitasDAO fasilitasDAO = new FasilitasDAOImpl();
            listFasilitas = fasilitasDAO.getDataByEquals(Fasilitas.class, "kamar.id", kamar.getId());
            updateTableFasilitas();
        } catch (Exception ex) {
            Logger.getLogger(FasilitasPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableFasilitas() {
        String[] judul = {"No", "Nama", "Tarif"};
        Object[][] isi = new Object[listFasilitas.size()][3];
        int x = 0;
        int no = 0;
        for (Fasilitas fasilitass : listFasilitas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = fasilitass.getItemFasilitas().getNama();
            isi[x][2] = numberFormat.format(fasilitass.getItemFasilitas().getTarif());
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableFasilitas);
    }
}
